module cl {
    export class Util {
        /**
         * 最顶层的图层实例
         */
        public static topMost : eui.UILayer;

        /**
         * 构造一个数组，并用某个值或某个构造函数对每一项进行初始化
         * new Array and fill value
         */
        public static createArray(length : number, value : any, paramIsConstructor : boolean = false) : any[] {
            let arr : any[] = [];
            if (paramIsConstructor){
                for (let idx = 0; idx < length; idx++){
                    arr.push(new value());
                }
            }else{
                for (let idx = 0; idx < length; idx++){
                    arr.push(value);
                }
            }
            return arr;
        };

        public static createSequenceNumberArray(start : number, length : number) : number[] {
            let arr : number[] = [];
            for (let i = 0; i < length; i++) {
                arr.push(start + i);
            }
            return arr;
        }

        /**
         * 构造一个数组，等于两个数组的公共部分
         */
        public static getIntersectArray(array1 : any[], array2 : any[]) : any[] {
            let array: any[] = [];
            for (let obj1 of array1){
                for (let obj2 of array2){
                    if (obj1 == obj2)
                        array.push(obj1);
                }
            }
            return array;
        };

        /**
         * 通过数组来给 new Class() 传参
         */
        public static newInstance(constructFunc, argsArray : any[]) : any {
            let args = [null].concat(argsArray);
            let factoryFunction = constructFunc.bind.apply(constructFunc, args);
            return new factoryFunction();
        }

        /**
         * 具有 x, y 的 Tile, Coord，xy相同
         */
        public static sameXY(a:any, b:any) : boolean {
            return a.x == b.x && a.y == b.y;
        }

        public static coordDegreeOffset(x : number, y : number, degree : number, distance : number) : Coord {
            let rad = degree / 180 * Math.PI;
            return new Coord(x + Math.cos(rad) * distance, y + Math.sin(rad) * distance);
        }
        public static coordRadOffset(x : number, y : number, rad : number, distance : number) : Coord {
            return new Coord(x + Math.cos(rad) * distance, y + Math.sin(rad) * distance);
        }

        /**
         * 设置单位颜色，r g b 均为 0~1之间的实数
         */
        public static setColor(obj: egret.DisplayObject, r: number, g: number, b: number, a: number = 1.0) : void {
            let colorFilter = new egret.ColorMatrixFilter([
                r, 0, 0, 0, 0,
                0, g, 0, 0, 0,
                0, 0, b, 0, 0,
                0, 0, 0, a, 0
            ]);
            obj.filters = [colorFilter];
        }

        /**
         * 设置单位朝向
         */
        public static setOrientation(obj: egret.DisplayObject, orientationIdx :number) : void {
            let tmp = orientationIdx > 4.5 ? orientationIdx - 7.5 : orientationIdx - 1.5;
            if (tmp < 0) {
                obj.rotation = -25 * (tmp + 1.5);
                obj.skewY = 180;
            } else {
                obj.rotation = -25 * (tmp - 1.5);
                obj.skewY = 0;
            }
        }

        /**
         * 设置单位位置，并以中心点为锚点
         */
        public static setLocation(obj: egret.Bitmap, coord : Coord) : void {
            obj.x = coord.x;
            obj.y = coord.y;
            obj.anchorOffsetX = 0.5 * obj.width;
            obj.anchorOffsetY = 0.5 * obj.height;
        }

        /**
         * 生成单位，设置单位位置，并以中心点为锚点
         */
        public static setImageCoord(res : string, coord : Coord) : egret.Bitmap {
            let obj = new egret.Bitmap(RES.getRes(res));
            obj.x = coord.x;
            obj.y = coord.y;
            obj.anchorOffsetX = 0.5 * obj.width;
            obj.anchorOffsetY = 0.5 * obj.height;
            return obj;
        }

        /**
         * 生成单位，设置单位位置，并以中心点为锚点
         */
        public static setImageTid(res : string, tid : number) : egret.Bitmap {
            let coord = Map.tileEngine.tid2coord(tid);
            let obj = new egret.Bitmap(RES.getRes(res));
            obj.x = coord.x;
            obj.y = coord.y;
            obj.anchorOffsetX = 0.5 * obj.width;
            obj.anchorOffsetY = 0.5 * obj.height;
            return obj;
        }

        /**
         * 设置组是否可Touch
         */
        public static setTouchable(isEnable : boolean, ...groups : eui.Group[]) : void {
            for (let group of groups) {
                group.touchEnabled = isEnable;
                group.touchChildren = isEnable;
                group.touchThrough = ! isEnable;
            }
        }

        /**
         * 设置组是否可Visible
         */
        public static setVisible(isVisible : boolean, ...groups : egret.DisplayObject[]) : void {
            for (let group of groups) {
                group.visible = isVisible;
            }
        }

        /**
         * 设置组是否可Enable
         */
        public static setEnable(isEnabled : boolean, ...buttons : any[]) : void {
            for (let button of buttons) {
                button.enabled = isEnabled;
                if (isEnabled){
                    Util.setColor(button, 1, 1, 1);
                } else {
                    Util.setColor(button, 0.75, 0.75, 0.75);
                }
            }
        }

        /**
         * 显示消息
         */
        public static message(msg : string, timeout : number  = 3000) : void {
            let label = new eui.Label(msg);
            label.name = "message_label";
            label.y = 50;
            label.size = 32;
            label.horizontalCenter = 0;
            label.textAlign = "center";
            label.fontFamily = "Microsoft YaHei";
            label.border = true;
            label.borderColor = 0xffffff;

            let oldLabel = Util.topMost.getChildByName("message_label");
            if (oldLabel != null) {
                Util.topMost.removeChild(oldLabel);
            }
            Util.topMost.addChild(label);

            setTimeout(() => {
                if (label.parent != null) {
                    label.parent.removeChild(label);
                }
            }, timeout);
        }
    }
}